// pages/address/address.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: [], //地址列表
    editAddressIndex: 0, //编辑时获取的索引值
    ordering: "",//从订单进来的标识
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    _this.queryAllAddress();
    if (options.ordering != undefined) {
      _this.setData({
        ordering: options.ordering,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.loginFlag) {
      this.queryAllAddress();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.globalData.otherPageHideFlag = true;
  },

  //查询收货地址
  queryAllAddress() {
    let _this = this;
    Api.queryAllAddress().then( res => {
      //console.log(res.data)
      _this.setData({
        addressList: res.data
      })
    })
  },

  //新建地址返回来的操作
  changeAddress: function () {
    let _this = this;
    _this.queryAllAddress()
  },

  //删除收货地址
  delAddress(e) {
    let _this = this;
    let index = e.currentTarget.dataset.index
    wx.showModal({
      content: '确认是否删除地址',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定');
          Api.delAddress(_this.data.addressList[index].id).then(res => {
            console.log(res)
            let arr = _this.data.addressList;
            arr.splice(index, 1)
            _this.setData({
              addressList: arr
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    

  },

  //设置默认的收货地址
  defaultAddress(e){
    let _this = this;
    let index = e.currentTarget.dataset.index;
   // console.log(_this.data.addressList[index].name)
    Api.defaultAddress(_this.data.addressList[index].id).then( res => {
      //console.log(res)
      _this.queryAllAddress();
      if (_this.data.ordering == "ordering") {
        let index = e.currentTarget.dataset.index;
        console.log(_this.data.addressList[index].id)
        let pages = getCurrentPages();
        let currPage = pages[pages.length - 1]; //当前页面
        let prevPage = pages[pages.length - 2]; //上一个页面
        prevPage.setData({
          addressId: _this.data.addressList[index].id
        });
        wx.navigateBack({});
        return;
      }
    })
  },

  //编辑收货地址
  editChange(e){
    let _this = this;
    let index = e.currentTarget.dataset.index;
    _this.setData({
      editAddressIndex: index,
    })
    wx.navigateTo({
      url: `/pages/address/editAddress?type=editAddeess&id=${_this.data.addressList[index].id}`,
    })
  },

  goOrdering(e) {
    console.log(111)
    let _this = this;
    if (_this.data.ordering == "ordering") {
      let index = e.currentTarget.dataset.index;
      console.log(_this.data.addressList[e.currentTarget.dataset.index].id)
      let pages = getCurrentPages();
      let currPage = pages[pages.length - 1]; //当前页面
      let prevPage = pages[pages.length - 2]; //上一个页面
      prevPage.setData({
        addressId: _this.data.addressList[e.currentTarget.dataset.index].id
      });
      wx.navigateBack({});
      return;
    }
  },


})